//
//  ViewController.swift
//  helloworld
//
//  Created by Mike on 2016/10/29.
//  Copyright © 2016年 GindaChen. All rights reserved.
//

import Cocoa
import CoreMIDI
import CoreText
import CoreAudio
import CoreAudioKit
import CoreMediaIO
import QuartzCore




class ViewController: NSViewController , NSTextFieldDelegate{
    
    
    @IBOutlet var textField: NSTextView!
    @IBOutlet weak var label: NSTextField!
    @IBOutlet weak var helper: NSButton!
    
    @IBOutlet weak var indicator_inf: NSProgressIndicator!
    @IBOutlet weak var indicator: NSProgressIndicator!
    @IBOutlet weak var pathindicator: NSPathCell!
    
    @IBOutlet weak var midiButton: NSButton!
    @IBOutlet weak var beatButton: NSButton!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        indicator.increment(by: 10)
        indicator_inf.startAnimation(nil)
        
        let thread = Thread( block:{
            sleep(2)
            self.indicator_inf.stopAnimation(nil)
        })
        thread.threadPriority = 0.2
        
        thread.start()
        pathindicator.backgroundColor = NSColor.brown
        
        let str = textField.textStorage?.string
        label.placeholderString = str;
        
    }
    
    
    @IBAction func saveFileAction(_ sender: AnyObject) {
        let savepanel = NSSavePanel()
        savepanel.allowsOtherFileTypes = true
        savepanel.canCreateDirectories = true
        savepanel.title = "Save"
        savepanel.begin { (result: Int) in
            
            if let path = savepanel.directoryURL{
                
                var name = savepanel.nameFieldStringValue
                
                if name.lengthOfBytes(using: String.Encoding.utf8) == 0 {
                    name = "Untitled.txt"
                }
                
                let url = URL(fileURLWithPath: name, relativeTo: path)
                
                if let str = self.textField.string{
                    do{
                        try str.write(to: url, atomically: true, encoding: String.Encoding.utf8)
                    }catch {
                        self.label.stringValue = "Exception"
                        return
                    }
                    self.label.stringValue = "Save Succeed"
                }else{
                    self.label.stringValue = "Save Denied"
                }
            }
            
        }
    }
    
    @IBAction func openFileAction(_ sender: AnyObject) {
        
        let openpanel = NSOpenPanel()
        openpanel.canCreateDirectories = true
        openpanel.canChooseFiles = true
        
        var toputonfile : String!
        
        openpanel.begin { (result : Int) in
            if result != 0 {
                self.label.stringValue = "Great! Opened the file"
                var str : String
                
                for url in openpanel.urls{
                    do{
                        str = try String(contentsOf: url, encoding: String.Encoding.utf8)
                    }catch{
                        self.label.stringValue = "Failed input"
                        return
                    }
                    self.textField.insertText(str, replacementRange: NSRange(location: 0, length: (self.textField.string?.lengthOfBytes(using: String.Encoding.utf8))!))
                }
            }else{
                self.label.stringValue = "Canceled opening"
            }
            
            if let i = toputonfile?.lengthOfBytes(using: String.Encoding.utf8) {
                self.label.stringValue = "Success input"
            }else{
                self.label.stringValue = "Empty input"
            }
        }
        
    }
    
    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }
    
    
    
    
    
    
    
    
    
    
    
}

